package com.stoneframe.base.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;

import java.net.InetSocketAddress;

import javax.annotation.Resource;

public class UDPServer {
	
	private final ChannelType channelType;
	private final InetSocketAddress localAddress;
	private Channel acceptorChannel;
	
	@Resource
	ServerUDPHandler udpHandler;
	
	public UDPServer(ChannelType channelType,int port){
		super();
		this.channelType=channelType;
		this.localAddress=new InetSocketAddress(port);
	}
	
	public void start(){
		acceptorChannel=ServerUDPChannelFactory.createAcceptorChannel(channelType, 
				localAddress, udpHandler);
	}
	
	public void stop(){
		if(acceptorChannel!=null){
			acceptorChannel.close().addListener(ChannelFutureListener.CLOSE);
		}
	}
	
	public void restart(){
		stop();
		start();
	}

}
